const tbody = document.querySelector('tbody')

// 渲染分数管理页面
async function getScore(){
  const res = await axios({url: './score/list'})
  console.log(res)
  const k = Object.keys(res.data.data)
  tbody.innerHTML = k.map(ele => `
  <tr>
    <th scope="row">${ele}</th>
    <td>${res.data.data[ele].name}</td>
    ${res.data.data[ele].score.map((e,index) => `<td data-id=${ele}  data-batch=${index + 1} class="score">${e}</td>`).join('')}
  </tr>
  `).join('')
}

getScore()

let ipt
// 添加点击事件
tbody.addEventListener('click', (e) => {
  if (ipt) {
    ipt.remove()
  }
  ipt = document.createElement('input')
  ipt.classList.add('inp')
  e.target.append(ipt)
  ipt.focus()
  ipt.addEventListener('focus',function(){
    ipt.remove()
  })
  ipt.addEventListener('keydown',async (ele) => {
    if (ele.key === 'Enter'){
      try {
        const res =  await axios ({
          url: './score/entry',
          method: 'POST',
          data: {
            stu_id: e.target.dataset.id, 
            batch: e.target.dataset.batch, 
            score: ele.target.value
          }
        })
      } catch (err) {
        console.dir(err);
        Toast.fail(err.response.data.message)
      }
      getScore()
    }
  })
})


